﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GUI.BaoCao.BCChiTietBanHangTheoHangHoa
{
    public partial class frmChiTietBanHangTheoHangHoa : Form
    {
        #region khởi tạo biến
        bool checkClose = false;
        DateTime date;
        DateTime datesosanh;
        DateTime batdau;
        DateTime ketthuc;
        Entities.HangHoa[] hanghoa;
        Entities.HDBanBuon[] hdbanbuon;
        Entities.HDBanLe[] hdbanle;
        Entities.ChiTietHDBanBuon[] chitiethdbanbuon;
        Entities.ChiTietHDBanle[] chitiethdbanle;
        Entities.KhoHang[] khohang;
        Entities.ChiTietKhoHang[] chitietkhohang;
        Entities.GoiHang[] goihang;
        Entities.ChiTietGoiHang[] chitietgoihang;
        ArrayList array;
        ArrayList array1;
        string makho = "";
        string tenkho = "";
        string loaihoadon = "";
        Entities.BCChiTietBanHangTheoHangHoa[] hienthi;
        Entities.BCChiTietBanHangTheoHangHoa[] baocao;
        Entities.BCChiTietBanHangTheoHangHoa save;
        System.Windows.Forms.SaveFileDialog savefile;
        int i = 0;
        #endregion

        public frmChiTietBanHangTheoHangHoa()
        {
            InitializeComponent();
            try
            {
                try
                {
                    Entities.CaiDatKho kho = Entitystatic.ViewFile();
                    makho = kho.MaKho;
                    tenkho = kho.TenKho;
                    if (makho.Length == 0 || makho == "NULL")
                    {
                        makho = "";
                        tenkho = "";
                    }
                }
                catch
                {
                }
                date = Entitystatic.date;
                batdau = ketthuc = date;
                SelectData();
                loaihoadon = cbbLoai.Text;
                cbbKho.Text = tenkho;
                lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
                savefile = new System.Windows.Forms.SaveFileDialog();
            }
            catch
            {
            }
        }

        private void frmChiTietBanHangTheoHangHoa_Load(object sender, EventArgs e)
        {
            XuLy();
        }

        private void fix()
        {

            try
            {
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.ReadOnly = true;
                dgvHienThi.Columns["KhoHang"].Visible = true;
                dgvHienThi.Columns["ChungTu"].Visible = true;
                dgvHienThi.Columns["NgayChungTu"].Visible = true;
                dgvHienThi.Columns["KhachHang"].Visible = true;
                dgvHienThi.Columns["TenHang"].Visible = true;
                dgvHienThi.Columns["DonGia"].Visible = true;
                dgvHienThi.Columns["SL"].Visible = true;
                dgvHienThi.Columns["VAT"].Visible = true;
                dgvHienThi.Columns["KhuyenMai"].Visible = true;
                dgvHienThi.Columns["GiaTriTT"].Visible = true;

                dgvHienThi.Columns["KhoHang"].HeaderText = "Kho Hàng";
                dgvHienThi.Columns["ChungTu"].HeaderText = "Mã Hóa Đơn";
                dgvHienThi.Columns["NgayChungTu"].HeaderText = "Ngày Lập";
                dgvHienThi.Columns["KhachHang"].HeaderText = "Tên Khách Hàng";
                dgvHienThi.Columns["TenHang"].HeaderText = "Mã Hàng Hóa";
                dgvHienThi.Columns["DonGia"].HeaderText = "Đơn Giá";
                dgvHienThi.Columns["SL"].HeaderText = "Số Lượng";
                dgvHienThi.Columns["VAT"].HeaderText = "VAT(%)";
                dgvHienThi.Columns["KhuyenMai"].HeaderText = "KM(%)";
                dgvHienThi.Columns["GiaTriTT"].HeaderText = "Doanh Thu";
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dgvHienThi.AllowUserToAddRows = false;
                dgvHienThi.AllowUserToDeleteRows = false;
                dgvHienThi.AllowUserToResizeRows = false;
                dgvHienThi.RowHeadersVisible = false;

            }
            catch
            {
            }
        }

        void SelectData()
        {
            hdbanbuon = Common.StaticVariable.InterfaceObject.SelectHDBanBuon();
            if (hdbanbuon == null)
                hdbanbuon = new Entities.HDBanBuon[0];
            chitiethdbanbuon = Common.StaticVariable.InterfaceObject.SelectChiTietHDBanBuon();
            if (chitiethdbanbuon == null)
                chitiethdbanbuon = new Entities.ChiTietHDBanBuon[0];
            hdbanle = Common.StaticVariable.InterfaceObject.SelectHDBanLe();
            if (hdbanle == null)
                hdbanle = new Entities.HDBanLe[0];
            chitiethdbanle = Common.StaticVariable.InterfaceObject.SelectChiTietHDBanLe();
            if (chitiethdbanle == null)
                chitiethdbanle = new Entities.ChiTietHDBanle[0];
            Entities.KhoHang kh = new Entities.KhoHang();
            kh.HanhDong = "1";
            kh.MaKho = "";
            khohang = Common.StaticVariable.InterfaceObject.SelectKhoHang(kh);
            if (khohang == null)
                khohang = new Entities.KhoHang[0];

            Entities.KhoHang[] temp = new Entities.KhoHang[khohang.Length + 1];
            if (temp.Length != 1)
            {
                int sotang = 0;
                foreach (Entities.KhoHang item in khohang)
                {
                    temp[sotang] = item;
                    sotang++;
                }
                temp[temp.Length - 1] = new Entities.KhoHang();
                temp[temp.Length - 1].MaKho = "Tất cả";
                temp[temp.Length - 1].TenKho = "Tất cả";
                Common.Utilities.XuLyComboBox(cbbKho, temp, Common.Constants.KhoHang.TenKho, Common.Constants.KhoHang.MaKho);
            }
            else
            {
                Common.Utilities.XuLyComboBox(cbbKho, khohang, Common.Constants.KhoHang.TenKho, Common.Constants.KhoHang.MaKho);
            }

        }

        #region In
        void In()
        {
            if (hienthi.Length - 2 == 0)
            {
                return;
            }
            try
            {
                Entities.BCChiTietBanHangTheoHangHoa[] temp = new Entities.BCChiTietBanHangTheoHangHoa[hienthi.Length - 1];
                int sotang = 0;
                for (int j = 0; j < hienthi.Length - 2; j++)
                {
                    temp[sotang] = hienthi[j];
                    sotang++;
                }
                frmBaoCaorpt frm = new frmBaoCaorpt(temp, makho, loaihoadon, batdau, ketthuc);
                if (frm.kiemtra)
                {
                    frm.ShowDialog();
                }
            }
            catch
            {
            }
        }
        #endregion

        #region xử lý
        void XuLy()
        {
            if (makho == "")
            {

                return;
            }
            if (cbbLoai.SelectedIndex == 2)
            {
                XuLyTatCa();
            }
            if (cbbLoai.SelectedIndex == 1)
            {
                XuLyBanBuon();
            }
            if (cbbLoai.SelectedIndex == 0)
            {
                XuLyBanLe();
            }

        }
        #endregion

        #region tất cả
        void XuLyTatCa()
        {
            double tongtien = 0;
            // lấy hàng hóa trong kho
            if (cbbKho.SelectedIndex == khohang.Length)
            {
                hanghoa = Common.StaticVariable.InterfaceObject.SelectHangHoa();
                if (hanghoa == null)
                    hanghoa = new Entities.HangHoa[0];
                goihang = Common.StaticVariable.InterfaceObject.SelectGoiHang(new Entities.GoiHang("1"));
                if (goihang == null)
                    goihang = new Entities.GoiHang[0];
                chitietgoihang = Common.StaticVariable.InterfaceObject.SelectChiTietGoiHang(new Entities.ChiTietGoiHang("1"));
                if (chitietgoihang == null)
                    chitietgoihang = new Entities.ChiTietGoiHang[0];
                array = new ArrayList();
                foreach (Entities.HangHoa item in hanghoa)
                {
                    array.Add(item);
                }
                Entities.HangHoa[] hh1 = Common.Utilities.CheckGoiHang(hanghoa, goihang, chitietgoihang);
                foreach (Entities.HangHoa item in hh1)
                {
                    array.Add(item);
                }
                hanghoa = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
            }
            else
            {
                Entities.ChiTietKhoHang kh = new Entities.ChiTietKhoHang();
                kh.HanhDong = "HangHoaTheoKho";
                kh.MaKho = makho;
                hanghoa = Common.StaticVariable.InterfaceObject.Select_HangHoa(kh);
                if (hanghoa == null)
                    hanghoa = new Entities.HangHoa[0];
                goihang = Common.StaticVariable.InterfaceObject.SelectGoiHang(new Entities.GoiHang("1"));
                if (goihang == null)
                    goihang = new Entities.GoiHang[0];
                chitietgoihang = Common.StaticVariable.InterfaceObject.SelectChiTietGoiHang(new Entities.ChiTietGoiHang("1"));

                if (chitietgoihang == null)
                    chitietgoihang = new Entities.ChiTietGoiHang[0];
                array = new ArrayList();
                foreach (Entities.HangHoa item in hanghoa)
                {
                    array.Add(item);
                }
                Entities.HangHoa[] hh1 = Common.Utilities.CheckGoiHang(hanghoa, goihang, chitietgoihang);
                foreach (Entities.HangHoa item in hh1)
                {
                    array.Add(item);
                }
                hanghoa = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
            }
            array = new ArrayList();
            array1 = new ArrayList();
            int sotang = 0;

            foreach (Entities.HangHoa item in hanghoa)
            {
                foreach (Entities.HDBanLe item1 in hdbanle)
                {
                    if (cbbKho.SelectedIndex == khohang.Length)
                    {
                        bool kt = false;
                        if (item1.NgayLap.Date >= batdau.Date && item1.NgayLap.Date <= ketthuc)
                        {
                            foreach (Entities.ChiTietHDBanle item2 in chitiethdbanle)
                            {
                                if (item1.MaHDBanLe == item2.MaHDBanLe && item2.MaHangHoa == item.MaHangHoa)
                                {
                                    Entities.BCChiTietBanHangTheoHangHoa tem = new Entities.BCChiTietBanHangTheoHangHoa();
                                    tem.STT = sotang + 1;
                                    tem.KhachHang = item.TenHangHoa;
                                    tem.KhoHang = makho;
                                    tem.ChungTu = item1.MaHDBanLe;
                                    tem.DonGia = item2.DonGia;
                                    tem.KhuyenMai = item2.PhanTramChietKhau;
                                    tem.MaHang = item2.MaHangHoa;
                                    tem.NgayChungTu = item1.NgayLap.ToString("dd/MM/yyyy");
                                    tem.SL = item2.SoLuong.ToString();
                                    tem.ThanhTien = double.Parse(item2.ThanhTien);
                                    tem.VAT = item2.ThueGTGT;
                                    array.Add(tem);
                                    kt = true;
                                    tongtien += tem.ThanhTien;
                                    sotang++;
                                }
                                if (kt)
                                    break;
                            }
                        }
                    }
                    else
                    {
                        bool kt = false;
                        if (item1.MaKho == makho && item1.NgayLap.Date >= batdau.Date && item1.NgayLap.Date <= ketthuc)
                        {
                            foreach (Entities.ChiTietHDBanle item2 in chitiethdbanle)
                            {
                                if (item1.MaHDBanLe == item2.MaHDBanLe && item2.MaHangHoa == item.MaHangHoa)
                                {
                                    Entities.BCChiTietBanHangTheoHangHoa tem = new Entities.BCChiTietBanHangTheoHangHoa();
                                    tem.STT = sotang + 1;
                                    tem.KhachHang = item.TenHangHoa;
                                    tem.KhoHang = makho;
                                    tem.ChungTu = item1.MaHDBanLe;
                                    tem.DonGia = item2.DonGia;
                                    tem.KhuyenMai = item2.PhanTramChietKhau;
                                    tem.MaHang = item2.MaHangHoa;
                                    tem.NgayChungTu = item1.NgayLap.ToString("dd/MM/yyyy");
                                    tem.SL = item2.SoLuong.ToString();
                                    tem.ThanhTien = double.Parse(item2.ThanhTien);
                                    tem.VAT = item2.ThueGTGT;
                                    array.Add(tem);
                                    kt = true;
                                    tongtien += tem.ThanhTien;
                                    sotang++;
                                }
                                if (kt)
                                    break;
                            }
                        }
                    }
                }
            }

            foreach (Entities.HangHoa item in hanghoa)
            {
                foreach (Entities.HDBanBuon item1 in hdbanbuon)
                {
                    if (cbbKho.SelectedIndex == khohang.Length)
                    {
                        bool kt = false;
                        if (item1.NgayLap.Date >= batdau.Date && item1.NgayLap.Date <= ketthuc)
                        {
                            foreach (Entities.ChiTietHDBanBuon item2 in chitiethdbanbuon)
                            {
                                if (item1.MaHDBanBuon == item2.MaHDBanBuon && item2.MaHangHoa == item.MaHangHoa)
                                {
                                    Entities.BCChiTietBanHangTheoHangHoa tem = new Entities.BCChiTietBanHangTheoHangHoa();
                                    tem.STT = sotang + 1;
                                    tem.KhachHang = item.TenHangHoa;
                                    tem.KhoHang = makho;
                                    tem.ChungTu = item1.MaHDBanBuon;
                                    tem.DonGia = item2.DonGia;
                                    tem.KhuyenMai = item2.PhanTramChietKhau;
                                    tem.MaHang = item2.MaHangHoa;
                                    tem.NgayChungTu = item1.NgayLap.ToString("dd/MM/yyyy");
                                    tem.SL = item2.SoLuong.ToString();
                                    tem.ThanhTien = double.Parse(item2.ThanhTien);
                                    tem.VAT = item2.ThueGTGT;
                                    array.Add(tem);
                                    kt = true;
                                    tongtien += tem.ThanhTien;
                                    sotang++;
                                }
                                if (kt)
                                    break;
                            }
                        }
                    }
                    else
                    {
                        bool kt = false;
                        if (item1.MaKho == makho && item1.NgayLap.Date >= batdau.Date && item1.NgayLap.Date <= ketthuc)
                        {
                            foreach (Entities.ChiTietHDBanBuon item2 in chitiethdbanbuon)
                            {
                                if (item1.MaHDBanBuon == item2.MaHDBanBuon && item2.MaHangHoa == item.MaHangHoa)
                                {
                                    Entities.BCChiTietBanHangTheoHangHoa tem = new Entities.BCChiTietBanHangTheoHangHoa();
                                    tem.STT = sotang + 1;
                                    tem.KhachHang = item.TenHangHoa;
                                    tem.KhoHang = makho;
                                    tem.ChungTu = item1.MaHDBanBuon;
                                    tem.DonGia = item2.DonGia;
                                    tem.KhuyenMai = item2.PhanTramChietKhau;
                                    tem.MaHang = item2.MaHangHoa;
                                    tem.NgayChungTu = item1.NgayLap.ToString("dd/MM/yyyy");
                                    tem.SL = item2.SoLuong.ToString();
                                    tem.ThanhTien = double.Parse(item2.ThanhTien);
                                    tem.VAT = item2.ThueGTGT;
                                    array.Add(tem);
                                    kt = true;
                                    tongtien += tem.ThanhTien;
                                    sotang++;
                                }
                                if (kt)
                                    break;
                            }
                        }
                    }
                }
            }

            hienthi = new Entities.BCChiTietBanHangTheoHangHoa[array.Count + 1];
            if (array.Count != 0)
            {
                sotang = 0;
                Entities.BCChiTietBanHangTheoHangHoa[] te = (Entities.BCChiTietBanHangTheoHangHoa[])array.ToArray(typeof(Entities.BCChiTietBanHangTheoHangHoa));
                foreach (Entities.BCChiTietBanHangTheoHangHoa item in te)
                {
                    hienthi[sotang] = item;
                    sotang++;
                }
            }
            hienthi[hienthi.Length - 1] = new Entities.BCChiTietBanHangTheoHangHoa();
            hienthi[hienthi.Length - 1].MaHang = "Tổng tiền";
            hienthi[hienthi.Length - 1].ThanhTien = tongtien;
            dgvHienThi.DataSource = hienthi;fix();
            
        }
        #endregion

        #region bán lẻ

        void XuLyBanLe()
        {
            double tongtien = 0;
            // lấy hàng hóa trong kho
            if (cbbKho.SelectedIndex == khohang.Length)
            {
                hanghoa = Common.StaticVariable.InterfaceObject.SelectHangHoa();
                if (hanghoa == null)
                    hanghoa = new Entities.HangHoa[0];
                goihang = Common.StaticVariable.InterfaceObject.SelectGoiHang(new Entities.GoiHang("1"));
                if (goihang == null)
                    goihang = new Entities.GoiHang[0];
                chitietgoihang = Common.StaticVariable.InterfaceObject.SelectChiTietGoiHang(new Entities.ChiTietGoiHang("1"));
                if (chitietgoihang == null)
                    chitietgoihang = new Entities.ChiTietGoiHang[0];
                array = new ArrayList();
                foreach (Entities.HangHoa item in hanghoa)
                {
                    array.Add(item);
                }
                Entities.HangHoa[] hh1 = Common.Utilities.CheckGoiHang(hanghoa, goihang, chitietgoihang);
                foreach (Entities.HangHoa item in hh1)
                {
                    array.Add(item);
                }
                hanghoa = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
            }
            else
            {
                Entities.ChiTietKhoHang kh = new Entities.ChiTietKhoHang();
                kh.HanhDong = "HangHoaTheoKho";
                kh.MaKho = makho;
                hanghoa = Common.StaticVariable.InterfaceObject.Select_HangHoa(kh);
                if (hanghoa == null)
                    hanghoa = new Entities.HangHoa[0];
                goihang = Common.StaticVariable.InterfaceObject.SelectGoiHang(new Entities.GoiHang("1"));
                if (goihang == null)
                    goihang = new Entities.GoiHang[0];
                chitietgoihang = Common.StaticVariable.InterfaceObject.SelectChiTietGoiHang(new Entities.ChiTietGoiHang("1"));

                if (chitietgoihang == null)
                    chitietgoihang = new Entities.ChiTietGoiHang[0];
                array = new ArrayList();
                foreach (Entities.HangHoa item in hanghoa)
                {
                    array.Add(item);
                }
                Entities.HangHoa[] hh1 = Common.Utilities.CheckGoiHang(hanghoa, goihang, chitietgoihang);
                foreach (Entities.HangHoa item in hh1)
                {
                    array.Add(item);
                }
                hanghoa = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
            }
            array = new ArrayList();
            array1 = new ArrayList();
            int sotang = 0;

            foreach (Entities.HangHoa item in hanghoa)
            {
                foreach (Entities.HDBanLe item1 in hdbanle)
                {
                    if (cbbKho.SelectedIndex == khohang.Length)
                    {
                        if (item1.NgayLap.Date >= batdau.Date && item1.NgayLap.Date <= ketthuc)
                        {
                            foreach (Entities.ChiTietHDBanle item2 in chitiethdbanle)
                            {
                                if (item1.MaHDBanLe == item2.MaHDBanLe && item2.MaHangHoa.ToUpper() == item.MaHangHoa.ToUpper())
                                {
                                    XuLyObject(item, item1, item2);
                                    tongtien += double.Parse(item2.ThanhTien);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (item1.MaKho == makho && item1.NgayLap.Date >= batdau.Date && item1.NgayLap.Date <= ketthuc)
                        {
                            foreach (Entities.ChiTietHDBanle item2 in chitiethdbanle)
                            {
                                if (item1.MaHDBanLe == item2.MaHDBanLe && item2.MaHangHoa.ToUpper() == item.MaHangHoa.ToUpper())
                                {
                                    XuLyObject(item, item1, item2);
                                    tongtien += double.Parse(item2.ThanhTien);
                                    sotang++;
                                }
                            }
                        }
                    }
                }
            }
            hienthi = new Entities.BCChiTietBanHangTheoHangHoa[array.Count + 2];
            if (array.Count != 0)
            {
                sotang = 0;
                Entities.BCChiTietBanHangTheoHangHoa[] te = (Entities.BCChiTietBanHangTheoHangHoa[])array.ToArray(typeof(Entities.BCChiTietBanHangTheoHangHoa));
                foreach (Entities.BCChiTietBanHangTheoHangHoa item in te)
                {
                    hienthi[sotang] = item;
                    sotang++;
                }
            }
            hienthi[hienthi.Length - 1] = new Entities.BCChiTietBanHangTheoHangHoa();
            hienthi[hienthi.Length - 1].TenHang = "Tổng tiền";
            hienthi[hienthi.Length - 1].GiaTriTT = (Common.Utilities.FormatMoney(tongtien.ToString()));
            hienthi[hienthi.Length - 2] = new Entities.BCChiTietBanHangTheoHangHoa();
            hienthi[hienthi.Length - 2].TenHang = "Tổng chiết khấu toàn hóa đơn";
            hienthi[hienthi.Length - 2].GiaTriTT = "0";
            dgvHienThi.DataSource = hienthi;fix();
        }

        void XuLyObject(Entities.HangHoa item, Entities.HDBanLe item1, Entities.ChiTietHDBanle item2)
        {
            Entities.BCChiTietBanHangTheoHangHoa tem = new Entities.BCChiTietBanHangTheoHangHoa();
            tem.TenHang = item.TenHangHoa;
            tem.KhachHang = item1.MaKhachHang;
            tem.KhoHang = makho;
            tem.ChungTu = item1.MaHDBanLe;
            tem.DonGia = Common.Utilities.FormatMoney(item2.DonGia);
            tem.KhuyenMai = item2.PhanTramChietKhau;
            tem.MaHang = item2.MaHangHoa;
            tem.NgayChungTu = item1.NgayLap.ToString("dd/MM/yyyy");
            tem.SL = item2.SoLuong.ToString();
            tem.ThanhTien = double.Parse(Common.Utilities.FormatMoney(item2.ThanhTien));
            tem.GiaTriTT = Common.Utilities.FormatMoney(item2.ThanhTien);
            tem.VAT = item2.ThueGTGT;
            array.Add(tem);
        }

        #endregion

        #region bán buôn

        double cktm = 0;
        void XuLyBanBuon()
        {
            cktm = 0;
            double tongtien = 0;
            // lấy hàng hóa trong kho
            if (cbbKho.SelectedIndex == khohang.Length)
            {
                hanghoa = Common.StaticVariable.InterfaceObject.SelectHangHoa();
                if (hanghoa == null)
                    hanghoa = new Entities.HangHoa[0];
                goihang = Common.StaticVariable.InterfaceObject.SelectGoiHang(new Entities.GoiHang("1"));
                if (goihang == null)
                    goihang = new Entities.GoiHang[0];
                chitietgoihang = Common.StaticVariable.InterfaceObject.SelectChiTietGoiHang(new Entities.ChiTietGoiHang("1"));
                if (chitietgoihang == null)
                    chitietgoihang = new Entities.ChiTietGoiHang[0];
                array = new ArrayList();
                foreach (Entities.HangHoa item in hanghoa)
                {
                    array.Add(item);
                }
                Entities.HangHoa[] hh1 = Common.Utilities.CheckGoiHang(hanghoa, goihang, chitietgoihang);
                foreach (Entities.HangHoa item in hh1)
                {
                    array.Add(item);
                }
                hanghoa = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
            }
            else
            {
                Entities.ChiTietKhoHang kh = new Entities.ChiTietKhoHang();
                kh.HanhDong = "HangHoaTheoKho";
                kh.MaKho = makho;
                hanghoa = Common.StaticVariable.InterfaceObject.Select_HangHoa(kh);
                if (hanghoa == null)
                    hanghoa = new Entities.HangHoa[0];
                goihang = Common.StaticVariable.InterfaceObject.SelectGoiHang(new Entities.GoiHang("1"));
                if (goihang == null)
                    goihang = new Entities.GoiHang[0];
                chitietgoihang = Common.StaticVariable.InterfaceObject.SelectChiTietGoiHang(new Entities.ChiTietGoiHang("1"));

                if (chitietgoihang == null)
                    chitietgoihang = new Entities.ChiTietGoiHang[0];
                array = new ArrayList();
                foreach (Entities.HangHoa item in hanghoa)
                {
                    array.Add(item);
                }
                Entities.HangHoa[] hh1 = Common.Utilities.CheckGoiHang(hanghoa, goihang, chitietgoihang);
                foreach (Entities.HangHoa item in hh1)
                {
                    array.Add(item);
                }
                hanghoa = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
            }
            array = new ArrayList();
            array1 = new ArrayList();
            int sotang = 0;

            foreach (Entities.HangHoa item in hanghoa)
            {
                foreach (Entities.HDBanBuon item1 in hdbanbuon)
                {
                    bool kt = false;
                    if (cbbKho.SelectedIndex == khohang.Length)
                    {
                        if (item1.NgayLap.Date >= batdau.Date && item1.NgayLap.Date <= ketthuc)
                        {
                            foreach (Entities.ChiTietHDBanBuon item2 in chitiethdbanbuon)
                            {
                                if (item1.MaHDBanBuon == item2.MaHDBanBuon && item2.MaHangHoa.ToUpper() == item.MaHangHoa.ToUpper())
                                {
                                    XuLyObject(item, item1, item2);
                                    tongtien += double.Parse(item2.ThanhTien);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (item1.MaKho == makho && item1.NgayLap.Date >= batdau.Date && item1.NgayLap.Date <= ketthuc)
                        {
                            foreach (Entities.ChiTietHDBanBuon item2 in chitiethdbanbuon)
                            {
                                if (item1.MaHDBanBuon == item2.MaHDBanBuon && item2.MaHangHoa.ToUpper() == item.MaHangHoa.ToUpper())
                                {
                                    XuLyObject(item, item1, item2);
                                    tongtien += double.Parse(item2.ThanhTien);
                                }
                            }
                        }
                    }
                }
            }
            hienthi = new Entities.BCChiTietBanHangTheoHangHoa[array.Count + 2];
            if (array.Count != 0)
            {
                sotang = 0;
                Entities.BCChiTietBanHangTheoHangHoa[] te = (Entities.BCChiTietBanHangTheoHangHoa[])array.ToArray(typeof(Entities.BCChiTietBanHangTheoHangHoa));
                foreach (Entities.BCChiTietBanHangTheoHangHoa item in te)
                {
                    hienthi[sotang] = item;
                    sotang++;
                }
            }
            hienthi[hienthi.Length - 1] = new Entities.BCChiTietBanHangTheoHangHoa();
            hienthi[hienthi.Length - 1].TenHang = "Tổng tiền";
            hienthi[hienthi.Length - 1].GiaTriTT = (Common.Utilities.FormatMoney(tongtien.ToString()));
            hienthi[hienthi.Length - 2] = new Entities.BCChiTietBanHangTheoHangHoa();
            hienthi[hienthi.Length - 2].TenHang = "Tổng chiết khấu toàn hóa đơn";
            hienthi[hienthi.Length - 2].GiaTriTT = (Common.Utilities.FormatMoney(cktm.ToString()));
            dgvHienThi.DataSource = hienthi;fix();
        }

        void XuLyObject(Entities.HangHoa item, Entities.HDBanBuon item1, Entities.ChiTietHDBanBuon item2)
        {
            Entities.BCChiTietBanHangTheoHangHoa tem = new Entities.BCChiTietBanHangTheoHangHoa();

            tem.TenHang = item.TenHangHoa;
            tem.KhachHang = item1.MaKhachHang;
            tem.KhoHang = makho;
            tem.ChungTu = item1.MaHDBanBuon;
            tem.DonGia = Common.Utilities.FormatMoney(item2.DonGia);
            tem.KhuyenMai = item2.PhanTramChietKhau;
            tem.MaHang = item2.MaHangHoa;
            tem.NgayChungTu = item1.NgayLap.ToString("dd/MM/yyyy");
            tem.SL = item2.SoLuong.ToString();
            tem.ThanhTien = double.Parse(Common.Utilities.FormatMoney(item2.ThanhTien));
            tem.CKTM = (tem.ThanhTien * item1.PhanTramChietKhauThuongmai) / 100;
            cktm += tem.CKTM;
            tem.GiaTriTT = Common.Utilities.FormatMoney(item2.ThanhTien);
            tem.VAT = item2.ThueGTGT;
            array.Add(tem);
        }

        #endregion

        #region Lọc điều kiện
        void LocDieuKien()
        {
            try
            {
                GUI.LocDieuKien.frmLocDieuKien frm = new LocDieuKien.frmLocDieuKien();
                frm.ShowDialog();
                if (GUI.LocDieuKien.frmLocDieuKien.kiemtra)
                {
                    DateTime truoc1;
                    DateTime sau1;
                    try
                    {
                        truoc1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.truoc);
                        sau1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.sau);
                    }
                    catch
                    {
                        return;
                    }
                    batdau = truoc1;
                    ketthuc = sau1;
                    XuLy();
                    GUI.LocDieuKien.frmLocDieuKien.kiemtra = false;
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Word
        void Word()
        {
            if (hienthi.Length - 2 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "Word |*.doc"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCChiTietBanHangTheoHangHoa[] temp = new Entities.BCChiTietBanHangTheoHangHoa[hienthi.Length - 1];
                    int sotang = 0;
                    for (int j = 0; j < hienthi.Length - 2; j++)
                    {
                        temp[sotang] = hienthi[j];
                        sotang++;
                    }
                    frmBaoCaorpt frm = new frmBaoCaorpt(temp, makho, loaihoadon, batdau, ketthuc, savefile.FileName, Common.Constants.Word);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Excel
        void Excel()
        {
            if (hienthi.Length - 2 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "Excel |*.xls"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCChiTietBanHangTheoHangHoa[] temp = new Entities.BCChiTietBanHangTheoHangHoa[hienthi.Length - 1];
                    int sotang = 0;
                    for (int j = 0; j < hienthi.Length - 2; j++)
                    {
                        temp[sotang] = hienthi[j];
                        sotang++;
                    }
                    frmBaoCaorpt frm = new frmBaoCaorpt(temp, makho, loaihoadon, batdau, ketthuc, savefile.FileName, Common.Constants.Excel);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region PDF
        void PDF()
        {
            if (hienthi.Length - 2 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "PDF |*.pdf"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCChiTietBanHangTheoHangHoa[] temp = new Entities.BCChiTietBanHangTheoHangHoa[hienthi.Length - 1];
                    int sotang = 0;
                    for (int j = 0; j < hienthi.Length - 2; j++)
                    {
                        temp[sotang] = hienthi[j];
                        sotang++;
                    }
                    frmBaoCaorpt frm = new frmBaoCaorpt(temp, makho, loaihoadon, batdau, ketthuc, savefile.FileName, Common.Constants.PDF);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            try
            {
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    checkClose = true;
                    this.Close();
                }
            }
            catch
            {
            }
        }
        #endregion

        private void frmChiTietBanHangTheoHangHoa_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (!checkClose)
                    if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        e.Cancel = checkClose;
                    }
                    else
                        e.Cancel = !checkClose;
            }
            catch
            {
            }
        }

        private void cbbKho_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbKho.SelectedIndex < 0)
                return;
            loaihoadon = cbbLoai.Text;
            makho = cbbKho.SelectedValue.ToString();
            tenkho = cbbKho.Text;
            XuLy();
        }

        private void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                i = e.RowIndex;
                save = ((Entities.BCChiTietBanHangTheoHangHoa[])dgvHienThi.DataSource)[i];
            }
        }

        private void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            In();
        }

        private void cbbLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            XuLy();
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            In();
        }

        private void btnLocDieuKien_Click(object sender, EventArgs e)
        {
            LocDieuKien();
        }

        private void btnWord_Click(object sender, EventArgs e)
        {
            Word();
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            Excel();
        }

        private void btnPDF_Click(object sender, EventArgs e)
        {
            PDF();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }


    }
}
